﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Admin_Package_EditRoute : System.Web.UI.Page
{
    BLLPackageManagement bllPackageManagement = new BLLPackageManagement();
    string barcode;
    private List<tblBranch> _branches;
    public List<tblBranch> lstBranch
    {
        get
        {
            if (_branches == null)
            {
                _branches = bllPackageManagement.ListBranches();
            }
            return _branches;
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {

        barcode = Request.QueryString["barcode"];
//        barcode = "13082012000080";

        if (!IsPostBack)
        {
            if (!string.IsNullOrEmpty(barcode))
            {
                bindTransit(barcode);
            }
            else
            {
                Response.Redirect("~/Admin/Package/ListPackage.aspx");
            }
        }
    }

    private void bindTransit(string barcode)
    {
        tblPackage package = bllPackageManagement.GetPackage(barcode);

        if (package != null)
        {
            int packageid = package.ID;

            List<vTransitRecord> lst = bllPackageManagement.ListTransitByPackageID(packageid);

            gvTransit.DataSource = lst;
            gvTransit.DataBind();

            if (gvTransit.Rows.Count > 0)
            {
                (gvTransit.Rows[0].FindControl("btnDelete") as LinkButton).Visible = false;
            }
        }
    }

    protected void btnEdit_Click(object sender, EventArgs e)
    {
        //dlvTransit.EditItemIndex = 0;
        //bindTransit(barcode);
    }

    
    protected void gvTransit_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "AddTransit")
        {
            int transitid = Convert.ToInt16(e.CommandArgument);
            tblTransit transit = bllPackageManagement.GetTransit(transitid);
            if (transit != null)
            {
                hidCurrentTransit.Value = transit.ID.ToString();
                lblCurrentBranchName.Text = transit.tblBranch.Name;
                panelNew.Visible = true;
            }
            bindTransit(barcode);
        }
        else if (e.CommandName == "DelTransit")
        {
            int transitid = Convert.ToInt16(e.CommandArgument);
            
            bllPackageManagement.DelTransit(transitid);
            
            bindTransit(barcode);
        }

    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        int _currentTransitID = Convert.ToInt16(hidCurrentTransit.Value);
        tblTransit transit = bllPackageManagement.GetTransit(_currentTransitID);
        if (transit != null)
        {
            tblTransit newTransit = new tblTransit()
            {
                PackageID = transit.PackageID,
                BranchID = Convert.ToInt16(ddlBranch.SelectedValue),
                Status = 0,
            };

            if (transit.NextTransitID != 0)
            {
                newTransit.NextTransitID = transit.NextTransitID;
            }

            bllPackageManagement.NewTransit(newTransit);
            transit.NextTransitID = newTransit.ID;
            bllPackageManagement.UpdateTransit();

            panelNew.Visible = false;
        }


        bindTransit(barcode);
    }

    protected void gvTransit_RowEditing(object sender, GridViewEditEventArgs e)
    {
        gvTransit.EditIndex = e.NewEditIndex;
        bindTransit(barcode);
    }
    protected void gvTransit_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        DropDownList ddl = gvTransit.Rows[e.RowIndex].FindControl("ddlBranch") as DropDownList;
        HiddenField hid = gvTransit.Rows[e.RowIndex].FindControl("hidID") as HiddenField;

        if (ddl != null && hid != null)
        {
            int transitid = Convert.ToInt16(hid.Value);
            int branchid = Convert.ToInt16(ddl.SelectedValue);

            tblTransit transit = bllPackageManagement.GetTransit(transitid);
            if (transit != null)
            {
                transit.BranchID = branchid;
            }
            bllPackageManagement.UpdateTransit();
        }
        gvTransit.EditIndex = -1;
        bindTransit(barcode);
    }
    protected void gvTransit_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        gvTransit.EditIndex = -1;
        bindTransit(barcode);
    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        panelNew.Visible = false;
        lblCurrentBranchName.Text = "";
        bindTransit(barcode);
    }
    protected void gvTransit_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {

    }
}